package com.wulis.generator.entrance;

import cn.hutool.core.collection.CollectionUtil;
import com.wulis.generator.code.*;
import com.wulis.generator.config.DatasourceConfig;
import com.wulis.generator.config.ProjectConfig;
import com.wulis.generator.config.TableConfig;
import com.wulis.generator.model.Table;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 代码生成入口
 * 
 * @author WuliBao
 */
@Slf4j
public class GeneratorEntrance {

    /**
     * 代码生成
     * 
     * @param datasourceConfig datasourceConfig
     * @param projectConfig projectConfig
     * @param tableConfig tableConfig
     */
    public static void generator(DatasourceConfig datasourceConfig, ProjectConfig projectConfig,
        TableConfig tableConfig) {
        log.info("---------------code generator start---------------");
        // 不生成CRUD的表
        List<Table> simpleTableList = tableConfig.getTables().stream().filter(t -> !t.isCrud()).distinct().toList();
        if (CollectionUtil.isNotEmpty(simpleTableList)) {
            String[] tableNames = simpleTableList.stream().map(Table::getName).toList().toArray(String[]::new);
            // 生成entity、service和mapper，不包括增删改查
            SimpleGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("simple code generator success");
        }
        // 生成CRUD的表
        List<Table> crudTableList = tableConfig.getTables().stream().filter(Table::isCrud).distinct().toList();
        if (CollectionUtil.isNotEmpty(crudTableList)) {
            String[] tableNames = crudTableList.stream().map(Table::getName).toList().toArray(String[]::new);
            // 生成CRUD需要的对象 PageReq PageRes GetInfoRes InsertReq UpdateReq DeleteReq
            PageReqGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("page request object code generator success");
            PageResGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("page response object code generator success");
            GetInfoResGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("get info request object code generator success");
            InsertReqGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("insert request object code generator success");
            UpdateReqGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("update request object code generator success");
            DeleteReqGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("delete request object code generator success");
            // 生成实体类
            EntityGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("entity code generator success");
            // 生成mapper
            MapperGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("mapper code generator success");
            // 生成service
            ServiceGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("service code generator success");
            // 生成controller
            ControllerGenerator.config(datasourceConfig, projectConfig, tableNames, tableConfig.getPrefix()).execute();
            log.info("controller code generator success");
        }
        log.info("---------------code generator end---------------");
    }
}
